package vue;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import modele.Plateau;
import outils.ImageOutils;

/**
 * Représente un mini map
 *
 * @author gervaila
 */
public class MiniMap extends JPanel {

	private AireDeJeu aire;
	private BufferedImage bufferCarte;
	private BufferedImage bufferApercu;
	private float scale;
	private int moveX;
	private int moveY;

	/**
	 *
	 */
	public MiniMap() {
	}

	/**
	 * Crée une miniMap en fonction de l'aire de jeu 'aire'
	 *
	 * @param aire l'aire de jeu que doit représenter la miniMap
	 */
	public void creerMiniMap(AireDeJeu aire) {
		this.aire = aire;
		Point tailleApercu = new Point(getPreferredSize().width, getPreferredSize().height);

		bufferCarte = aire.getBufferCarte();
		bufferApercu = new BufferedImage(tailleApercu.x, tailleApercu.y, BufferedImage.TYPE_INT_ARGB);

		scale = Math.min((float) tailleApercu.x / bufferCarte.getWidth(), (float) tailleApercu.y / bufferCarte.getHeight());
		moveX = 0;
		moveY = 0;
		if (bufferCarte.getWidth() < bufferCarte.getHeight()) {
			scale = (float) tailleApercu.y / bufferCarte.getHeight();
			moveX = (int) (tailleApercu.x - (scale * bufferCarte.getWidth())) / 2;
		} else {
			scale = (float) tailleApercu.x / bufferCarte.getWidth();
			moveY = (int) (tailleApercu.y - (scale * bufferCarte.getHeight())) / 2;
		}
	}

	/**
	 * Actualise la miniMap
	 */
	public void actualiserMiniMap() {
		BufferedImage pions = new BufferedImage(bufferCarte.getWidth(), bufferCarte.getHeight(), BufferedImage.TYPE_INT_ARGB);
		BufferedImage filtreCamera = new BufferedImage(bufferCarte.getWidth(), bufferCarte.getHeight(), BufferedImage.TYPE_INT_ARGB);
		BufferedImage apercuTmp = ImageOutils.deepCopy(bufferApercu);

		Graphics2D gApercu = (Graphics2D) apercuTmp.createGraphics();
		Graphics2D gPions = (Graphics2D) pions.createGraphics();
		Graphics2D gFiltre = (Graphics2D) filtreCamera.createGraphics();

		int transparence = 150;

		Camera camera = aire.getCamera();
		Rectangle rect = camera.getRectangle();

		dessinerPions(gPions);


		gFiltre.setColor(new Color(50, 50, 50, transparence));
		gFiltre.setBackground(new Color(255, 255, 255, 0));
		gFiltre.fillRect(0, 0, bufferCarte.getWidth(), bufferCarte.getHeight());

		gFiltre.clearRect(rect.x, rect.y, rect.width, rect.height);

		AffineTransform tx = new AffineTransform();
		tx.setTransform(scale, 0, 0, scale, moveX, moveY);

		gApercu.drawImage(bufferCarte, tx, null);
		gApercu.drawImage(pions, tx, this);
		gApercu.drawImage(filtreCamera, tx, null);

		JLabel picLabel = new JLabel(new ImageIcon(apercuTmp));
		removeAll();
		ImageOutils.ajouterJLabelPanel(this, picLabel);
	}

	/*
	 * Dessin les pions sur la miniMap
	 */
	private void dessinerPions(Graphics2D gPions) {
		Plateau p = aire.getPlateau();
		for (int x = 0; x < p.getLargeur(); x++) {
			for (int y = 0; y < p.getHauteur(); y++) {
				if (p.getCase(x, y).getPion() != null) {
					Color couleur = p.getCase(x, y).getPion().getJoueur().getCouleurOpaque();
					gPions.setColor(couleur);
					gPions.fillOval(x * Plateau.TAILLE_CASE, y * Plateau.TAILLE_CASE, Plateau.TAILLE_CASE, Plateau.TAILLE_CASE);
				}
			}
		}
	}
}
